package v2;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

public class JdPageProcessor extends ShangCheng implements PageProcessor{
	
	private static final Logger log = Logger.getLogger(JdPageProcessor.class);
	
	private Site site = Site.me().setCharset("gbk")
			.setSleepTime(1000);
	
	
	private List<String> skuIds;
	
	private String entryUrl;
	
	private int num;
	
	public String getEntryUrl() {
		return entryUrl;
	}

	public void setEntryUrl(String entryUrl) {
		this.entryUrl = entryUrl;
	}

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	@Override
	public void process(Page page) {
		String urls = page.getUrl().get();
		if(urls.contains("list.jd.com/list.html")){
			if(pageTotal == 0){
				String pageTotalStr = page.getHtml().xpath("//span[@class='p-skip']/allText()").toString();
				// 确定总页数
				String regEx = "[^0-9]";
				Pattern p = Pattern.compile(regEx);
				try{
					Matcher m = p.matcher(pageTotalStr);
					pageTotal = Integer.valueOf(m.replaceAll("").trim());
				}catch(Exception e){
					pageTotal = -1;
				}
				// 根据页数，添加需要的url
				for (int i = 2; i <= pageTotal; i++) {
					Request request = new Request();
					request.setUrl(entryUrl+"&page="+i);
					page.addTargetRequest(request);
				}
			}
			//搜索获取商品列表
			searchGdsList(page);
		}else if(page.getUrl().regex("http://item.jd.com/\\d+.html").toString() != null){
			//获取商品下的所有skuId
			genSkus(page);
		}else if(page.getUrl().regex("http://p.3.cn/prices/get?.*") != null){
			//获取sku的相关信息
			genSkuInfo(page);
		}
	}
	
	private void searchGdsList(Page page){
		Selectable xpath = page.getHtml().xpath("//*[@class='gl-item']");
		List<Selectable > nodes = xpath.nodes();
		for(Selectable s : nodes){
			String urla = s.xpath("//[@class='p-name']/a/@href").get();
			Request request = new Request(urla).setPriority(0);
			page.addTargetRequest(request);
			num++;
		}
		page.setSkip(true);
	}
	
	private void genSkus(Page page){
		
		skuIds = new ArrayList<>();
		String gdsInfoJson = null;
		String[] lines = page.getHtml().xpath("//script/html()").toString().split("\n");
		for(String line : lines){
			if (line.contains("colorSize:") || line.contains("colorSize :")){
				String[] params = line.split("  ");
				for(String param : params){
					if (param.contains("colorSize:") || param.contains("colorSize :")){
						gdsInfoJson=param.substring(0,param.length()-1).substring(param.indexOf(":")+1).trim();
						break;
					}
				}
			}
		}
		
		//获取json串中所有的SkuId
		try{
			if("{}".equals(gdsInfoJson)){
				//有些商品没有属性，跳过
			}else{
				JSONArray jsonArray = JSONArray.parseArray(gdsInfoJson);
				this.skuIds = new ArrayList<>();
				for(int i=0;i<jsonArray.size();i++){
					JSONObject jsonObject = jsonArray.getJSONObject(i);
					String skuId = jsonObject.get("SkuId").toString();
					String color = jsonObject.getString("Color");
					String size = jsonObject.getString("Size");
					if(StringUtils.isNotBlank(skuId)){
						Request request = new Request();
						Map<String, Object> map = new HashMap<>();
						map.put("skuId", skuId);
						map.put("color", color);
						map.put("size", size);
						request.setExtras(map);
						request.setUrl("http://item.jd.com/"+skuId+".html");
						request.setPriority(1);
						page.addTargetRequest(request);
					}
				}
			}
		}catch(Exception e){
			System.out.println("json格式有误");
		}
		
		String skuId = String.valueOf(page.getRequest().getExtra("skuId")); 
		if(StringUtils.isBlank(skuId) || "null".equals(skuId)){
			String url =  page.getUrl().toString();
			skuId = url.substring(url.lastIndexOf("/")+1, url.lastIndexOf("."));
		}
		String color = String.valueOf(page.getRequest().getExtra("color"));
		String size = String.valueOf(page.getRequest().getExtra("size"));
		String title = page.getHtml().xpath("//title/html()").toString();
		String seller = page.getHtml().xpath("//[@class='seller-infor']/em/allText()").get();
		List<Selectable> nodes = page.getHtml().xpath("////*[@class='Ptable']//tr").nodes();
		StringBuilder sb = new StringBuilder();
		if(nodes != null && nodes.size()>0){
			if(nodes != null && nodes.size()>0){
				for(Selectable s : nodes){
					String key = s.xpath("//td[1]/allText()").get();
					String value = s.xpath("//td[2]/allText()").get();
					if (key == null || value == null){
						continue;
					}
					sb.append(key.trim() + ":" + value.trim().replace(";", "_").replace(":", "_") + ";");
				}
			}
		}else{
			nodes = page.getHtml().xpath("//div[@class='Ptable']//div[@class='Ptable-item']//dl").nodes();
			if(nodes != null && nodes.size()>0){
				for(Selectable s : nodes){
					List<Selectable> dts = s.xpath("//dt").nodes();
					List<Selectable> dds = s.xpath("//dd").nodes();
					for(int i=0;i<dts.size();i++){
						String key = dts.get(i).xpath("//allText()").get();
						String value = dds.get(i).xpath("//allText()").get();
						if (key == null || value == null){
							continue;
						}
						sb.append(key.trim() + ":" + value.trim().replace(";", "_").replace(":", "_") + ";");
					}
					
				}
			}
		}
		if(StringUtils.isNotBlank(skuId)){
			Request request2 = new Request();
			request2.putExtra("seller", seller);
			request2.putExtra("title", title);
			request2.putExtra("skuId", skuId);
			request2.putExtra("color", color);
			request2.putExtra("size", size);
			request2.putExtra("detail", sb.toString());
			long timestamp = new Date().getTime();
			request2.setUrl("http://p.3.cn/prices/get?pduid="+timestamp+"&skuid=J_"+skuId);
			request2.setPriority(2);
			request2.setMethod("GET");
			page.addTargetRequest(request2);
			page.setSkip(true);
		}
	}
	
	private void genSkuInfo(Page page){
		String json = page.getJson().get();
		
		JSONArray jsonArray = JSONArray.parseArray(json);
		JSONObject jsonObject = JSONObject.parseObject(jsonArray.getString(0));
		String price = jsonObject.getString("p");
		if(StringUtils.isNotBlank(price)){
			page.putField("title", page.getRequest().getExtra("title"));
			page.putField("seller", page.getRequest().getExtra("seller"));
			page.putField("skuId", page.getRequest().getExtra("skuId"));
			page.putField("color", page.getRequest().getExtra("color"));
			page.putField("size", page.getRequest().getExtra("size"));
			page.putField("detail", page.getRequest().getExtra("detail"));
			page.putField("price", price);
		}
	}

	@Override
	public Site getSite() {
		return site;
	}
	
	public void setUserAgent(String userAgent){
		this.site.setUserAgent(userAgent);
	}
	
	public static void main(String[] args) throws Exception{
		/*String url = "http://list.jd.com/list.html?cat=9987,653,655&ev=exbrand_2032&go=0&stock=0";
		long startTime = new Date().getTime();
		JdPageProcessor jdPageProcessor = new JdPageProcessor();
		jdPageProcessor.setEntryUrl(url);
		String filePath = "D://baojia//v2//jd//";
		String fileName = "OPPO3.json";
		Spider spider = Spider.create(jdPageProcessor).scheduler(new PriorityScheduler())
				.addPipeline(new JsonFilePipeline(filePath, fileName));
		spider.addUrl(url);
		spider.thread(5).run();
		
		long endTime = new Date().getTime();
		System.out.println(jdPageProcessor.getNum());
		System.out.println("时间："+String.valueOf((endTime-startTime)));
		*/
		List<Integer> aIntegers= new ArrayList<>();
		aIntegers.add(3);
		System.out.println(aIntegers.get(2));
	}
}
